package cn.edu.njnu.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class EmailSender {
    @Value("${spring.mail.username}")
    private String sender;
    @Value("${registry.allowedOrigins}")
    private String hostUrl;
    @Autowired
    private JavaMailSender javaMailSender;
    public void sendVerifyMsg(String emailAddress,String uuid){
        log.info("sending mail from: {} to: {}", sender, emailAddress);
        SimpleMailMessage smm = new SimpleMailMessage();
        smm.setFrom(sender);
        smm.setTo(emailAddress);
        smm.setSubject("智慧学伴");
        smm.setText("欢迎使用，点击完成注册验证:"+hostUrl+"/e-resource/#/email?uuid="+uuid);
        javaMailSender.send(smm);
    }
    public void sendCode(String emailAddress,String code){
        log.info("sending mail from: {} to: {}", sender, emailAddress);
        SimpleMailMessage smm = new SimpleMailMessage();
        smm.setFrom(sender);
        smm.setTo(emailAddress);
        smm.setSubject("智慧学伴");
        smm.setText("您的验证码为:"+code+"\n有效期为5分钟。");
        javaMailSender.send(smm);
    }
}
